import { getCitys } from '../api/index'

export const getCurrentCity = () => {
  const localCity = JSON.parse(localStorage.getItem('hkzf_city'))

  if (!localCity) {
    return new Promise((resolve, reject) => {
      const myCity = new window.BMap.LocalCity()
      myCity.get(async data => {
        try {
          const res = await getCitys(data.name)
          localStorage.setItem('hkzf_city', JSON.stringify(res.data.body))
          resolve(res.data.body)
        } catch (e) {
          reject(e)
        }
      })
    })
  }

  return Promise.resolve(localCity)
}
